package com.example.myapplication.house;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.MyappealBean;
import com.example.myapplication.ShuJu.SearchhousingBean;
import com.example.myapplication.apter.BotYIzhanAdapter;
import com.example.myapplication.apter.SearchhousingAdapter;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HouseActivity extends AppCompatActivity {

    private ImageView house_img;
    private RecyclerView re_house;
    private SearchView house_search_badge;
    private TabLayout house_tabs;
    private Button but_house;
    private Request  request;
    private OkHttpClient  okHttpClient;
    private SearchhousingBean  searchhousingBean;
    private Gson gson;
    private Handler handler;
    private String houseDetailsActivityType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house);
        Intent intent = getIntent();
        houseDetailsActivityType = intent.getStringExtra("HouseDetailsActivityType");
        /*if(houseDetailsActivityType!=null){
            Searchhousing(houseDetailsActivityType);
        }else{
            Searchhousing("二手");
        }*/
        initview();
        //房源分类
        Housingclassificationlist();
        Searchhousing("二手");
    }



    private void Housingclassificationlist() {
        ArrayList<String>  Housinglist = new ArrayList<>();
        Housinglist.add("二手");
        Housinglist.add("租房");
        Housinglist.add("楼盘");
        Housinglist.add("中介");
        for (int i = 0; i < Housinglist.size(); i++) {
            TabLayout.Tab tab = house_tabs.newTab();
            tab.setText(Housinglist.get(i));
            house_tabs.addTab(tab);
        }
        house_tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                String tabtext = tab.getText().toString();
                Searchhousing(tabtext);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void Searchhousing(String tabtext) {
        request = new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/api/house/housing/list?houseType="+tabtext)
                .addHeader("Authorization", TokenBean.token)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                Log.d("Myappeal","Myappeal失败");
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                searchhousingBean = gson.fromJson(string,  SearchhousingBean.class);
                Log.d("Myappealstring","Myappealstring"+string);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        bingsearchhousing();
                    }
                });
            }
        });
    }

    private void bingsearchhousing() {
        SearchhousingAdapter searchhousingAdapter = new SearchhousingAdapter(HouseActivity.this,searchhousingBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        re_house.setLayoutManager(linearLayoutManager);
        re_house.setAdapter(searchhousingAdapter);
    }

    private void initview() {
        house_img = findViewById(R.id.house_img);
        house_tabs = findViewById(R.id.house_tabs);
        re_house = findViewById(R.id.re_house);
        house_search_badge = findViewById(R.id.house_search_badge);
        but_house = findViewById(R.id.but_house);

        okHttpClient= new OkHttpClient();
        gson = new Gson();
        handler = new Handler();
    }

   /* @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
            if(requestCode ==999){
                String type = data.getStringExtra("HouseDetailsActivityType");
                Searchhousing(type);
            }
        }
    }*/
}